引言
基于官方Jenkins镜像,构建打包环境,包括Maven/git/nodejs;
预安装jenkins各种插件;
具体见: https://github.com/smalldok/jenkins-maven-git-nodejs-docker
以下是构建jenkins image步骤
拉取官方镜像
启动docker服务
拉取jenkins镜像(docker hub 官方)
官方镜像搜索:https://hub.docker.comdocker pull jenkins:2.60.3
或者docker pull jenkins
- 查看镜像
docker images
- 删除镜像
docker rmi 7b210b6c238a
或者docker rmi -f 7b210b6c238a
- 运行镜像
1
docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v /Users/smalldok/work/idea-work/docker/volume/jenkins_home:/var/jenkins_home jenkins:2.60.3
或者以交互方式运行(这种适合更新修改镜像内部的东西)1
2
3docker run --name myjenkins -i -t -p 8080:8080 -p 50000:50000 -v /Users/smalldok/work/idea-work/docker/volume/jenkins_home:/var/jenkins_home jenkins:2.60.3 /bin/bash
然后exit退出、commit来重新创建镜像副本
或者后台方式运行(推荐)1
docker run --name myjenkins -d --env JAVA_OPTS="-Xmx8192m" -p 8080:8080 -p 50000:50000 -v /Users/smalldok/work/idea-work/docker/volume/jenkins_home:/var/jenkins_home jenkins:2.60.3
1 | docker run --name myjenkins -d --env JAVA_OPTS="-Xmx8192m" -p 8080:8080 -p 50000:50000 -v /Users/smalldok/work/idea-work/docker/volume/jenkins_home:/var/jenkins_home -e ROOT_PASS="jenkins" jenkins:2.60.3 |
- jenkins解锁
启动后访问http://127.0.0.1:8080/ 出现Unlock Jenkins页面,按提示在jenkins容器中获取密码;
命令行进入容器:1
2
3docker ps -l
docker exec -it 91e4b117c7f0 /bin/bash
cat /var/jenkins_home/secrets/initialAdminPassword
以root用户进入容器1
docker exec -it -u root myjenkins /bin/bash
制作Dockerfile
验证jenkins plugin安装
jenkins plugin list
- Organization and Administration
dashboard-view
cloudbees-folder
antisamy-markup-formatter
backup - Build Features
token-macro
build-timeout
credentials-binding
ws-cleanup
timestamper - Build Tools
nodejs
build-name-setter
scm-api 源码管理工具 - Build Analysis and Reporting
无 - Pipelines and Continuous Delivery
build-pipeline-plugin
parameterized-trigger
copyartifact - Source Code Management
git-client
git
gitlab-plugin
github-pullrequest - User Management and Security
matrix-auth
pam-auth
ldap
role-strategy - Notifications and Publishing
Publish Over SSH
ssh-credentials
SSH
Mailer
email-ext - project and view
nested-view
sectioned-view - docker
docker-commons
docker-build-publish
docker-plugin
kubernetes
jenkins plugin.txt
查找或下载插件 https://plugins.jenkins.io/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42dashboard-view:latest
cloudbees-folder:latest
antisamy-markup-formatter:latest
backup:latest
token-macro:latest
build-timeout:latest
credentials-binding:latest
ws-cleanup:latest
timestamper:latest
nodejs:latest
build-name-setter:latest
scm-api:latest
build-pipeline-plugin:latest
parameterized-trigger:latest
copyartifact:latest
git-client:latest
git:latest
gitlab-plugin:latest
github-pullrequest:latest
matrix-auth:latest
pam-auth:latest
ldap:latest
role-strategy:latest
publish-over-ssh:latest
ssh-credentials:latest
ssh:latest
mailer:latest
email-ext:latest
nested-view:latest
sectioned-view:latest
docker-commons:latest
docker-build-publish:latest
docker-plugin:latest
kubernetes:latest
验证插件安装
进入容器命令行
1
docker exec -it -u root myjenkins /bin/bash
准备plugins.txt
1
2cd /usr/share/jenkins/
vi plugins.txt执行插件安装脚本
1
/usr/local/bin/install-plugins.sh $(cat /usr/share/jenkins/plugins.txt | tr '\n' ' ')
单个失败,则重新安装,如:/usr/local/bin/install-plugins.sh jira-trigger:latest
安装完成后,查看/usr/share/jenkins/ref/plugins
目录;
- 登录jenkins 控制台页面查看
http://127.0.0.1:8080/pluginManager/installed验证JDK、GIT、SSH安装
JDK,默认在jenkins基础镜像中已经安装,见官方Dockerfile:https://github.com/jenkinsci/docker/blob/master/Dockerfile
GIT,默认在上面安装jenkins插件git时已经安装;
SSH,默认在上面安装jenkins插件ssh时已经安装;1
2
3java -version
git --version
ssh
验证maven安装
1 | apt-get update && apt-get install -y wget |
验证nodejs安装
1 | # get nodejs 9.9.0 |
Dockerfile
1 | # 基础镜像,基于官方jenkins2.60.3,包括openJDK8 |
Build image
1 | # build |
Push image
镜像上传阿里云:
- 注册阿里云docker registry https://cr.console.aliyun.com
- 在账户中创建命名空间、镜像仓库
- 登录阿里云docker registry
1
docker login --username=[用户名] registry.cn-hangzhou.aliyuncs.com
登录registry的用户名是您的阿里云账号全名,密码是您开通服务时设置的密码。
你可以在镜像管理首页点击右上角按钮修改docker login密码。
- 将镜像推送到registry
1
2
3$ docker login --username=[用户名] registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/smalldok/jenkins:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/smalldok/jenkins:[镜像版本号]
镜像上传docker官方(被墙)
- 注册Hub账号:
https://hub.docker.com/ - 在账户中创建仓库:Create Repository
login hub
1
docker login
upload
1
docker push smalldok/jenkins:1.0
其他命令
1
2# 修改镜像tag
docker tag <imageID> <namespace>/<image name>:<version tag eg latest>
常见问题
问题1
删除容器,会删除-v /your/home:/var/jenkins_home
指定挂载目录下的数据?
不会,例如:执行docker rm myjenkins
,并不会删除宿主机/your/home下的数据
问题2
构建Image Size过大问题?
一条指令构建一层layer,应该减少层即可减少体积;1
2
3RUN aaaaa
RUN bbbbb
RUN ccccc
应尽量改为:1
2
3RUN aaaaa \
&& bbbbb \
&& ccccc
问题3
docker: Error response from daemon: error creating overlay mount to /Users/smalldok/install/docker/docker-root-dir/overlay2/517ba0e3473b5196356d2c970322371964dff4c8803acc88421e71a34aabe783-init/merged: invalid argument.
不要修改docker root dir,还原为默认的/var/lib/docker下
问题4:
ocker: Error response from daemon: Conflict. The container name “/myjenkins” is already in use by container “e4c7f088c941063dfbea65c65ec683978dab2d309fa252a9f735594f6bb481d9”. You have to remove (or rename) that container to be able to reuse that name.
主机上运行docker rm xxxx 删除容器
问题5:
在容器中执行vi xxx.txt 报错 vi: command not found;
在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时,提示:1
2
3
4Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
这时候需要敲:apt-get update,这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
等更新完毕以后再敲命令:apt-get install vim命令即可。